/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditCustomer.java
 *
 * Created on Nov 5, 2011, 4:15:26 PM
 */
package mpinventory;

import java.sql.*;
import java.util.List;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 *
 * @author Sarah Kelsey
 */
public class EditCustomer extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    public CustomerQuery customerQuery;
    private List<Customer> customers;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    public boolean backToMainMenu=false;
    /** Creates new form EditUser */
    public EditCustomer(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        initComponents();
        size=this.getPreferredSize();
        width=size.width;
        height=size.height+35;
        initializeCustomerList();
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCECustID = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCEName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtCEAddress = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtCEConNum = new javax.swing.JTextField();
        btnCESave = new javax.swing.JButton();
        btnCECancel = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        txtCERemarks = new javax.swing.JTextField();

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25)); // NOI18N
        jLabel1.setText("Edit Customer Information");

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Customer ID:");

        txtCECustID.setEditable(false);
        txtCECustID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Name:");

        txtCEName.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Address:");

        txtCEAddress.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        txtCEAddress.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("<html>Contact<br> Number/s:</html>");

        txtCEConNum.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        btnCESave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnCESave.setText("Save");
        btnCESave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        btnCECancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnCECancel.setText("Cancel");
        btnCECancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel7.setText("Remarks:");

        txtCERemarks.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtCERemarks)
                                    .addComponent(txtCEAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                                    .addComponent(txtCEConNum, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                                    .addComponent(txtCECustID, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCEName, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(63, 63, 63)
                                .addComponent(btnCESave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(60, 60, 60)
                                .addComponent(btnCECancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(26, 26, 26))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(50, 50, 50))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCECustID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCEName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCEAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCEConNum, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel7))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(txtCERemarks, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCESave)
                    .addComponent(btnCECancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void initializeCustomerList(){
        customerQuery=new CustomerQuery(conn);
        customers=customerQuery.initializeCustomers();
        DefaultListModel listModel = new DefaultListModel();
        for(int i=0;i<customers.size();i++)
            listModel.addElement(customers.get(i).getName());
        listCustomer.setModel(listModel);
        int j=listCustomer.getSelectedIndex()+1; //+1 because index of lists starts at -1
        txtCEName.setText(customers.get(j).getName());
        txtCECustID.setText(customers.get(j).getID()+"");
        txtCEAddress.setText(customers.get(j).getAddress()+"");
        txtCEConNum.setText(customers.get(j).getContactNumbers()+"");
        txtCERemarks.setText(customers.get(j).getRemarks()+"");
        listCustomer.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e){
                try{
                    int j=listCustomer.getSelectedIndex();
                    customers=customerQuery.initializeCustomers();
                    txtCEName.setText(customers.get(j).getName());
                    txtCECustID.setText(customers.get(j).getID()+"");
                    txtCEAddress.setText(customers.get(j).getAddress()+"");
                    txtCEConNum.setText(customers.get(j).getContactNumbers()+"");
                    txtCERemarks.setText(customers.get(j).getRemarks()+"");
                }catch(Exception f){}
            }
        });
    }
    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
        Object[] options=new String[]{"Yes","No"};
        pane.setOptions(options);
        JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
        dialog.show();
        if(options[0].equals(pane.getValue())) //YES
            backToMainMenu=true;
    }//GEN-LAST:event_cancelActionPerformed

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        customerQuery=new CustomerQuery(conn);
        if(txtCEName.getText().equals("") || txtCEAddress.getText().equals("") || txtCEConNum.getText().equals("") || txtCERemarks.getText().equals(""))
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to edit this customer?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm Edition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                customerQuery.editCustomer(Integer.parseInt(txtCECustID.getText()),txtCEName.getText(),txtCEAddress.getText(),txtCEConNum.getText(),txtCERemarks.getText(),listCustomer.getSelectedValue().toString());
                int j=listCustomer.getSelectedIndex();
                DefaultListModel listModel = new DefaultListModel();
                customers=customerQuery.initializeCustomers();
                for(int i=0;i<customers.size();i++)
                    listModel.addElement(customers.get(i).getName());
                listCustomer.setModel(listModel);
                listCustomer.setSelectedIndex(j); //update customer list
            } //YES
        }
    }//GEN-LAST:event_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCECancel;
    private javax.swing.JButton btnCESave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField txtCEAddress;
    private javax.swing.JTextField txtCEConNum;
    private javax.swing.JTextField txtCECustID;
    private javax.swing.JTextField txtCEName;
    private javax.swing.JTextField txtCERemarks;
    // End of variables declaration//GEN-END:variables
}
